package com.NyCloud.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.NyCloud.entity.Demand;
import com.NyCloud.service.IDemandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-07-26
 */
@RestController
@RequestMapping("/demand")
@Api(tags = "生产需求")
@CrossOrigin
public class DemandController {

    @Autowired
    private IDemandService demandService;

    @ApiOperation("全部需求")
    @GetMapping("/all")
    public SaResult listAll() {
        return SaResult.data(demandService.list());
    }

    @ApiOperation("某个公司的需求")
    @GetMapping("/{companyId}")
    public SaResult getCompanyById(@PathVariable int companyId) {
        return SaResult.data(demandService.list(new QueryWrapper<Demand>()
                .eq("company_id", companyId)));
    }

    @PostMapping
    @ApiOperation("新增需求")
    @SaCheckLogin
    public SaResult addDemand(@RequestBody Demand demand) {
        demand.setCompanyId(StpUtil.getLoginIdAsInt());
        demandService.save(demand);
        return SaResult.data(demand.getId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除需求")
    public SaResult deleteDemand(@PathVariable String id) {
        demandService.removeById(Integer.parseInt(id));
        return SaResult.ok();
    }


}
